/*
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <http://www.gnu.org/licenses/>.
 */
package server;

import java.rmi.UnexpectedException;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

// Hlavni trida slouzici k sifrovani dat, ktera prochazeji pres sit. Metoda pouzita je DES.
public class Sifrovani
{
	private static String phrase = ""; // Promenna pro ulozeni sifrovaciho klice

	// vygenerovani sifrovaciho klice
	public static void generatePhrase()
	{
		phrase = Gen.generatePhrase();
	}

	// zasifrovani
	public static String encrypt(String text) throws UnexpectedException
	{
		if (phrase == "") // pokud sifrovaci klic neni vygenerovan
			throw new UnexpectedException("phrase is not set"); // vyhodi vyjimku

		if (text == null) // pokud vstupni text je null
			return null; // vrati null

		try
		{
			DESKeySpec keySpec = new DESKeySpec(phrase.getBytes("UTF8")); // pripravi DES klic z prvnich 8 bajtu sifrovaciho klice
			SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); // vytvori klice vybraneho sifrovaciho algoritmu
			SecretKey key = keyFactory.generateSecret(keySpec); // vytvori DES klic z pripraveneho materialu (DESKeySpec)
			sun.misc.BASE64Encoder base64encoder = new BASE64Encoder(); // Vytvori novou instanci encoderu
			byte[] cleartext = text.getBytes("UTF8"); // prevede text do pole bajtu
			Cipher cipher = Cipher.getInstance("DES"); // vytvoreni nove instane samotne sifrovaci funkce
			cipher.init(Cipher.ENCRYPT_MODE, key); // inicializace sifrovani: nastaveni modu (zasifrovani) a vlozeni klice
			return base64encoder.encode(cipher.doFinal(cleartext)); // samotne zasifrovani a navraceni
		}
		catch (Exception e)
		{
		}
		return null;
	}

	// odsifrovani
	public static String decrypt(String s) throws UnexpectedException
	{
		if (phrase == "") // pokud sifrovaci klic neni vygenerovan
			throw new UnexpectedException("phrase is not set"); // vyhodi vyjimku

		if (s == null) // pokud vstupni text je null
			return null; // vrati null

		try
		{
			sun.misc.BASE64Decoder base64decoder = new BASE64Decoder(); // Vytvori novou instanci decoderu
			byte[] encrypedPwdBytes = base64decoder.decodeBuffer(s); // Ziskani pole bajtu ze zasifrovane zpravy
			DESKeySpec keySpec = new DESKeySpec(phrase.getBytes("UTF8")); // pripravi DES klic z prvnich 8 bajtu sifrovaciho klice
			SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); // vytvori klice vybraneho sifrovaciho algoritmu
			SecretKey key = keyFactory.generateSecret(keySpec); // vytvori DES klic z pripraveneho materialu (DESKeySpec)
			Cipher cipher = Cipher.getInstance("DES"); // vytvoreni nove instane samotne sifrovaci funkce
			cipher.init(Cipher.DECRYPT_MODE, key); // inicializace sifrovani: nastaveni modu (odsifrovani) a vlozeni klice
			byte[] plainTextPwdBytes = (cipher.doFinal(encrypedPwdBytes)); // samotne odsifrovani
			return new String(plainTextPwdBytes); // prevod bajtu do stringu a navraceni
		}
		catch (Exception e)
		{
		}
		return null;
	}

	// metoda pro zjisteni sifrovaciho klice
	public static String getPhrase()
	{
		return phrase;
	}
}
